﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;

namespace AutoDeploy.FrontEnd
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class EachStep : Window
    {
        public ObservableCollection<AutoDeploy.Common.Library.CommandValuesView> ocCVV;
        private StringBuilder sb = new StringBuilder();
        private int CurrentStep = 0;
        private AutoDeploy.Common.Library.DeploymentObject ado;
        private ObservableCollection<Common.Library.CommandValuesView> oc;
        private bool hasStartMessageRun = false;
        public EachStep(ObservableCollection<Common.Library.CommandValuesView> oc, AutoDeploy.Common.Library.DeploymentObject deploymentObject)
        {
            InitializeComponent();
            ado = deploymentObject;
            this.Title = "Auto Deployer-" + ado.Name;
            this.oc = oc;
            butRun.IsEnabled = true;
            NextStep();
        }
        public void NextStep()
        {
            if (CurrentStep > 0)
            {
                if (CurrentStep <= ado.CommandSets.Count)
                {
                    SetStepFormProperties();
                }
                else
                {
                    EndMessage();
                }
            }
            else
            {
                if (!hasStartMessageRun)
                {
                    StartMessage();
                }
                SetStepFormProperties();
            }
        }
        private void EndMessage()
        {
            string s = "Email MarkWRowe@hotmail.com or post on autodeploy.codeplex.com if there are any problems or feature requests.\n";
            MessageBox.Show(s);
            sb.Append(s);
        }
        private void StartMessage()
        {
            string s = "Thank you for using Auto Deploy!\nPlease Check all of the commands before they are run.\n" +
                "This program allows for XML configuration file changes, Shell Processes,  Run Database Commands and Scripts, \n" +
                "It can also check installed programs and services and registry entries to be installed.\n" +
                "You choose your AutoDeploy.xml file to run, so please just review the commands run.\n" +
                "If you have not reviewed the AutoDeploy PLEASE TERMINATE and review the commands.\n" +
                "Check autodeploy.codeplex.com for updates. Thank you, Mark Rowe \n";
            MessageBox.Show(s);
            sb.Append(s);
            hasStartMessageRun = true;
            butRunAll.IsEnabled = (ado.FastDeploy);
            CurrentStep++;

        }
        private void LoadDataGrid()
        {
            ocCVV = new ObservableCollection<AutoDeploy.Common.Library.CommandValuesView>();
            if (ado.CommandSets.Count >= CurrentStep)
            {
                foreach (var v in oc.Where(x => x.Priority == CurrentStep))
                {
                    ocCVV.Add(v);
                }
                dgCommands.ItemsSource = ocCVV;
            }
        }
        private void SetStepFormProperties()
        {
            LoadDataGrid();
            SetButtons();
            this.Title = "Auto Deployer -" + ado.Name + " - " + (CurrentStep);
            tbStatus.Text += sb.ToString();
            sb.Clear();
        }
        private void SetButtons()
        {
            butBack.IsEnabled = (CurrentStep > 1);
            butNext.IsEnabled = (CurrentStep < ado.CommandSets.Count);
            butRun.IsEnabled = RunButtonEnabled();
        }
        private void butTerminate_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.Close();
            }
            catch (Exception ex)
            {
                //DoNothing;
            }
        }
        private void butNext_Click(object sender, RoutedEventArgs e)
        {
            CurrentStep++;
            NextStep();
        }
        private void butBack_Click(object sender, RoutedEventArgs e)
        {
            CurrentStep--;
            NextStep();
        }
        private void RunAllCommandsLeft()
        {
            int c = CurrentStep;
            for (int i = c; i < ado.CommandSets.Count; i++)
            {
             if (RunButtonEnabled())
                {
                    sb.Append(AutoDeploy.Common.Library.DeployHelper.RunCurrentCommand(ado.CommandSets[CurrentStep - 1], ado.RunAsUserName, ado.RunAsPassword, ado.SQLConnectionString));
                }
                else
                {
                    sb.Append(string.Format("One or more of the Run checkboxes are unchecked, AutoDeploy does not have the capability for PARTIAL Command runs."));
                }
            tbStatus.Text += sb.ToString();
            SetStepFormProperties();
            CurrentStep = i;
            }
        }
        private void butRunAll_Click(object sender, RoutedEventArgs e)
        {
            RunAllCommandsLeft();
        }

        private void butRun_Click(object sender, RoutedEventArgs e)
        {
            if (RunButtonEnabled())
            {
                sb.Append(AutoDeploy.Common.Library.DeployHelper.RunCurrentCommand(ado.CommandSets[CurrentStep - 1], ado.RunAsUserName, ado.RunAsPassword, ado.SQLConnectionString));
                tbStatus.Text += sb.ToString();
                MessageBox.Show(sb.ToString());
                sb.Clear();
            }
            butRun.IsEnabled = false;
        }
        private bool RunButtonEnabled()
        {

            //return (q.Count() == 0);
            if (ocCVV != null)
            {
                foreach (var item in ocCVV)
                {
                    if (!item.Run)
                    {
                        return false;
                    }
                }
                return true;
            }
            else
            {
                return false;
            }

        }

        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            butRun.IsEnabled = (RunButtonEnabled());
        }


    }
}
